package com.wit.paper.controller;

import com.wit.paper.exception.BusinessException;
import com.wit.paper.exception.ParameterException;
import com.wit.paper.model.Items;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Created by 何鹏帅 on 2016/11/23.
 */
@ControllerAdvice
public class BaseController {

    @ExceptionHandler
    @ResponseBody
    public Items exp(HttpServletRequest request, HttpServletResponse response, Exception e) {
        System.out.println("12334");
        Items items = new Items();
        response.setStatus(300);
        return items;
        //        if (request.getHeader("accept").indexOf("application/json") > -1
//                || (request.getHeader("X-Requested-With")!= null && request.getHeader("X-Requested-With").indexOf("XMLHttpRequest") > -1)) {
//            System.out.println("Exception caught");
//            response.setStatus(500);
//            response.setContentType("application/json;charset=utf-8");
//            try {
//                PrintWriter writer = response.getWriter();
//                writer.write(e.getMessage());
//                writer.flush();
//                StringWriter sw = new StringWriter();
//
//                e.printStackTrace(new PrintWriter(sw, true));
//
//                String str = sw.toString();
//                System.out.println(str);
//            } catch (IOException e1) {
//                // TODO Auto-generated catch block
//                e1.printStackTrace();
//            }
//            return items;
//        }
//        else{//如果是普通请求
//            request.setAttribute("exceptionMessage", e.getMessage());
//
//            return items;
////            // 根据不同的异常类型可以返回不同界面
////            if(e instanceof Exception)
////                return "testerror";
////            else
////                return "error";
//        }
    }
}
